package com.elong.spring.advice;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;

import com.elong.spring.proxy.PerformanceMonitor;

public class ControllablePerformanceMonitor extends DelegatingIntroductionInterceptor implements Monitorable{

    private ThreadLocal<Boolean> monitorStatusMap = new ThreadLocal<>();
    
    @Override
    public void setMonitorActive(boolean active) {
	monitorStatusMap.set(active);
    }
    
    public Object invoke(MethodInvocation mi) throws Throwable {
	Object obj = null;
	if(monitorStatusMap.get() != null && monitorStatusMap.get()){
	    PerformanceMonitor.begin(mi.getClass().getName() + "." + mi.getMethod().getName());
	    obj = super.invoke(mi);
	    PerformanceMonitor.end();
	}else {
	    obj = super.invoke(mi);
	}
	return obj;
    }

}
